﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace OpenXMLProcess
{
    public class RunItem
    {
        private XElement _element;

        public XElement Element { get { return _element; } set { _element = value; } }
        public XElement Format { 
            get 
            {
                if (_element.Element(MyDocument.w + "rPr")==null)
                {
                    _element.AddFirst(new XElement(MyDocument.w + "rPr", "<w:sz w:val='13' />"));
                }
                return _element.Element(MyDocument.w + "rPr");
            } 
        }
        public string Text
        {
            get
            {
                if (HasText())
                    return _element.Element(MyDocument.w + "t").Value;
                else if (HasTab())
                    return "\t";
                return "";
            }
            set
            {
                if(HasText())
                    _element.Element(MyDocument.w + "t").Value = value;
            }
        }
        public string TextFormat
        {
            get
            {
                if (HasText())
                    return _element.Element(MyDocument.w + "t").Value+" || "+Style;
                else if (HasTab())
                    return "\t";
                return "";
            }
        }
        public string Style
        {
            get
            {
                if (Format.Element(MyDocument.w + "rStyle") != null)
                    return Format.Element(MyDocument.w + "pStyle").Attribute(MyDocument.w + "val").Value;
                return "Normal";
            }
        }

        public RunItem(XElement element)
        {
            _element = element;
        }
        public bool HasText()
        {
            return _element.Element(MyDocument.w + "t") != null;
        }
        public bool HasTab()
        {
            return _element.Element(MyDocument.w + "tab") != null;
        }
        public bool IsRunItem()
        {
            return _element.Name.Equals(MyDocument.w + "r");
        }
    }
}
